#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=favicon.ico
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Description=ImageX Bootable CD Creation Utility
#AutoIt3Wrapper_Res_Fileversion=1.2.1.3
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=John Seekins
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_res_requestedExecutionLevel=requireAdministrator
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#comments-start
	Copyright (c) <2011>, <John Seekins>
	All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
	* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
	* Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#comments-end
#include <GUIConstants.au3>
#include <Constants.au3>
#include <GuiComboBox.au3>
#include <File.au3>

Opt("TrayIconHide", 1)

#comments-start Finding installed software
	$a_software=_InstalledSoftware()
	_ArrayDisplay($a_software)
	Func _InstalledSoftware()
	Local $Count = 1
	Local Const $regkey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'

	While 1
	$key = RegEnumKey($regkey, $Count)
	If @error <> 0 Then ExitLoop
	ConsoleWrite($key & @CRLF)
	$line = RegRead($regkey & '\' & $key, 'DisplayName')
	$line = StringReplace($line, ' (remove only)', '')
	If $line <> '' Then
	If Not IsDeclared('avArray') Then Dim $avArray[1][5]
	ReDim $avArray[UBound($avArray) + 1][5]
	$avArray[0][0] = UBound($avArray) - 1
	$avArray[UBound($avArray) - 1][0] = $line
	$avArray[UBound($avArray) - 1][1] = RegRead($regkey & '\' & $key, 'DisplayVersion')
	$avArray[UBound($avArray) - 1][2] = RegRead($regkey & '\' & $key, 'InstallDate')
	$avArray[UBound($avArray) - 1][3] = RegRead($regkey & '\' & $key, 'Publisher')
	$avArray[UBound($avArray) - 1][4] = $key
	EndIf
	$Count = $Count + 1
	WEnd
	If Not IsDeclared('avArray') Then
	SetError(1)
	Return ('')
	Else
	SetError(0)

	Return ($avArray)
	EndIf
	EndFunc   ;==>_InstalledSoftware
#comments-end

Dim $driveMapErrors[6]
$driveMapErrors[0] = "Undefined or Other"
$driveMapErrors[1] = "Access to the remote share was denied"
$driveMapErrors[2] = "The device is already assigned"
$driveMapErrors[3] = "Invalid device name"
$driveMapErrors[4] = "Invalid remote share"
$driveMapErrors[5] = "Invalid password"

;http://www.microsoft.com/downloads/thankyou.aspx?familyId=94bb6e34-d890-4932-81a5-5b50c657de08&displayLang=en
Dim $BaseX = 20
Dim $BaseY = 45
Dim $DestinationDir = StringLeft(@WindowsDir, 3)
Dim $fullDestination = ""
Dim $archDir = ""
Dim $servicingDir = ""
Dim $PEToolsDir = ""
Dim $customScriptsDir = ""
Dim $toolsDir = ""
Dim $infDir = ""
Dim $ISODir = ""
Dim $mountDir = ""
Dim $progressAmount = 0
Dim $bootImagePath = ""
Dim $sourceDir = @WorkingDir
Dim $folderArray = ""
Dim $logOutput = 0
Dim $mappedDrive = ""
Dim $imageFileLoc = ""
Dim $programTitle = "ImageX Boot Disc Creation Tools"
Dim $buildSafe = 1

;Dim $functionSuccessCodes[8][1]
;$functionSuccessCodes["DirCopy"][0] = 1
;$functionSuccessCodes["DirCreate"][0] = 1
;$functionSuccessCodes["DirRemove"][0] = 1
;$functionSuccessCodes["FileCopy"][0] = 1
;$functionSuccessCodes["FileRemove"][0] = 1
;$functionSuccessCodes["ImageX"][0] = 0
;$functionSuccessCodes["OSCDImg"][0] = 0
;$functionSuccessCodes["PEImg"][0] = 0

$hGUI = GUICreate($programTitle, 325, 250)
Opt("GUICoordMode", 1)

#region Drop-Down Menu
$fileMenu = GUICtrlCreateMenu("&File")
$exitProgramMenuItem = GUICtrlCreateMenuItem("E&xit", $fileMenu)
$helpMenu = GUICtrlCreateMenu("&Help")
$aboutMenuItem = GUICtrlCreateMenuItem("About...", $helpMenu)
#endregion Drop-Down Menu

#region ISO Creation
$hGUITab = GUICtrlCreateTab(5, 5, 315, 220)
$hGUIISOTab = GUICtrlCreateTabItem("ISO Creation")
$CurX = $BaseX
$CurY = $BaseY
GUICtrlCreateLabel("Source Directory: ", $CurX, $CurY)
$hSourceString = GUICtrlCreateInput($sourceDir, $CurX + 80, $CurY - 4, 140)
$hSourceButton = GUICtrlCreateButton("Browse...", $CurX + 230, $CurY - 6)
$CurY += 30
GUICtrlCreateLabel("Destination: ", $CurX, $CurY)
$hDestinationString = GUICtrlCreateInput($DestinationDir, $CurX + 70, $CurY - 4, 110)
$hDestinationButton = GUICtrlCreateButton("Browse...", $CurX + 190, $CurY - 6)
$CurY += 30
GUICtrlCreateLabel("Architecture: ", $CurX, $CurY)
$hArchCombo = GUICtrlCreateCombo("x86", $CurX + 70, $CurY - 4, 80, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "amd64")
$CurY += 65
$hBuildISOButton = GUICtrlCreateButton("&Build ISO", $CurX, $CurY)
$CurX += 75
$hLogOutputCheck = GUICtrlCreateCheckbox("Log Output", $CurX, $CurY)
#endregion ISO Creation
#region Networking
$hGUINetTab = GUICtrlCreateTabItem("Networking")
$CurX = $BaseX
$CurY = $BaseY

GUICtrlCreateLabel("Letter: ", $CurX, $CurY + 3)
$hMapLetter = GUICtrlCreateCombo("D:", $CurX + 70, $CurY, -1, -1, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "E:|F:|G:|H:|I:|J:|K:|L:|M:|N:|O:|P:|Q:|R:|S:|T:|U:|V:|W:|X:|Y:|Z:")

$CurY = $CurY + 27
GUICtrlCreateLabel("UNC Path: ", $CurX, $CurY + 3)
$hMapShareEdit = GUICtrlCreateInput("\\", $CurX + 70, $CurY, 140)

$CurY = $CurY + 27
GUICtrlCreateLabel("Username: ", $CurX, $CurY + 3)
$hMapUserEdit = GUICtrlCreateInput("", $CurX + 70, $CurY, 140)

$CurY = $CurY + 27
GUICtrlCreateLabel("Password: ", $CurX, $CurY + 3)
$hMapPasswordEdit = GUICtrlCreateInput("", $CurX + 70, $CurY, 140, -1, 0x0020)

$hMapButtonGo = GUICtrlCreateButton("&Map!", $CurX + 100, $CurY + 50)
#endregion Networking

GUISetState()
GUICtrlSetState($hLogOutputCheck, $GUI_UNCHECKED)

Do
	$msg = GUIGetMsg()
	Switch $msg
		Case $aboutMenuItem
			MsgBox(0, $programTitle, "Version: " & FileGetVersion("Image Creation.exe"))
		Case $hSourceButton
			$sourceDir = FileSelectFolder("Select source folder", "", 2, $sourceDir)
			If StringRight($sourceDir, 1) = "\" Then $sourceDir = StringTrimRight($sourceDir, 1)
			GUICtrlSetData($hSourceString, $sourceDir)
		Case $hDestinationButton
			$DestinationDir = FileSelectFolder("Select destination folder", $DestinationDir)
			If StringRight($DestinationDir, 1) = "\" Then $DestinationDir = StringTrimRight($DestinationDir, 1)
			GUICtrlSetData($hDestinationString, $DestinationDir)
		Case $hBuildISOButton
			$servicingDir = $sourceDir & "\Tools\Servicing"
			$PEToolsDir = $sourceDir & "\Tools\PETools"
			$customScriptsDir = $sourceDir & "\custom scripts"
			$toolsDir = $sourceDir & "\Tools\"
			$infDir = $sourceDir & "\Drivers"
			$folderArray = _FileListToArray($infDir, "*", 2)
			If GUICtrlRead($hLogOutputCheck) = $GUI_CHECKED Then
				If FileExists($DestinationDir & "\creationlog.txt") Then FileDelete($DestinationDir & "\creationlog.txt")
				$logOutput = 1
				$ret = FileOpen($DestinationDir & "\creationlog.txt", 2)
				FileWrite($DestinationDir & "\creationlog.txt", "Service Folder Directories:" & @CRLF & @CRLF)
				FileWrite($DestinationDir & "\creationlog.txt", $servicingDir & @CRLF)
				FileWrite($DestinationDir & "\creationlog.txt", $PEToolsDir & @CRLF)
				FileWrite($DestinationDir & "\creationlog.txt", $customScriptsDir & @CRLF)
				FileWrite($DestinationDir & "\creationlog.txt", $toolsDir & @CRLF)
				FileWrite($DestinationDir & "\creationlog.txt", $infDir & @CRLF)
				FileWrite($DestinationDir & "\creationlog.txt", $folderArray & @CRLF & @CRLF & @CRLF)
				FileClose($ret)
			Else
				$logOutput = 0
			EndIf
			modCallingWindow($programTitle, @SW_MINIMIZE)
			BuildISO()
			;modCallingWindow($programTitle, @SW_RESTORE)
		Case $hMapButtonGo
			DoMapping(GUICtrlRead($hMapLetter), GUICtrlRead($hMapShareEdit), GUICtrlRead($hMapUserEdit), GUICtrlRead($hMapPasswordEdit))
		Case $exitProgramMenuItem
			$msg = $GUI_EVENT_CLOSE
	EndSwitch
Until $msg = $GUI_EVENT_CLOSE

DriveMapDel($mappedDrive)
GUIDelete()

#region Networking Functions
Func DoMapping($mapLetter, $mapShare, $mapUser, $mapPassword)
	Local $ret = 0
	DriveMapDel($mapLetter)
	$ret = DriveMapAdd($mapLetter, $mapShare, 8, $mapUser, $mapPassword)

	If $ret Then
		$mappedDrive = $mapLetter
		MsgBox(64, "Mapping Drive", "Drive successfully mapped!")
	Else
		SplashOff()
		MsgBox(16, "Drive Mapping", "Error mapping drive: " & $driveMapErrors[@error])
	EndIf
EndFunc   ;==>DoMapping
#endregion Networking Functions

#region ISO Creation Functions
Func BuildISO()
	Local $ret = 0
	$fullDestination = $DestinationDir & "\winpe_" & GUICtrlRead($hArchCombo)
	$archDir = $PEToolsDir & "\" & GUICtrlRead($hArchCombo)
	$toolsDir &= GUICtrlRead($hArchCombo)
	$ISODir = $fullDestination & "\ISO"
	$mountDir = $fullDestination & "\mount"
	$bootImagePath = $fullDestination & "\etfsboot.com"
	If $logOutput Then
		FileWrite($DestinationDir & "\creationlog.txt", @CRLF & "Internal Service Folders and paths:" & @CRLF & @CRLF)
		FileWrite($DestinationDir & "\creationlog.txt", $fullDestination & @CRLF)
		FileWrite($DestinationDir & "\creationlog.txt", $archDir & @CRLF)
		FileWrite($DestinationDir & "\creationlog.txt", $toolsDir & @CRLF)
		FileWrite($DestinationDir & "\creationlog.txt", $ISODir & @CRLF)
		FileWrite($DestinationDir & "\creationlog.txt", $mountDir & @CRLF)
		FileWrite($DestinationDir & "\creationlog.txt", $bootImagePath & @CRLF & @CRLF)
	EndIf
	If MsgBox(1, "Beginning ISO Creation", "This will take some time. Please do not close the program.") = 1 Then
		ProgressOn("ISO File Creation", "Creating the ImageX ISO", "", -1, 0, 16)
		#region Copy necessary files
		$ret = DirCreate($ISODir)
		If $logOutput Then logProgress("DirCreate " & $ISODir, $ret)
		updateProgress("% -> Copying Necessary Files")
		$ret = DirCreate($mountDir)
		If $logOutput Then logProgress("DirCreate " & $mountDir, $ret)
		updateProgress("% -> Copying Necessary Files")
		$ret = DirCreate($ISODir & "\boot")
		If $logOutput Then logProgress("DirCreate " & $ISODir & "\boot", $ret)
		updateProgress("% -> Copying Necessary Files")
		$ret = DirCreate($ISODir & "\EFI")
		If $logOutput Then logProgress("DirCreate " & $ISODir & "\EFI", $ret)
		updateProgress("% -> Copying Necessary Files")
		$ret = DirCreate($ISODir & "\sources")
		If $logOutput Then logProgress("DirCreate " & $ISODir & "\sources", $ret)
		updateProgress("% -> Copying Necessary Files")
		$ret = FileCopy($archDir & "\bootmgr", $ISODir, 1)
		If $logOutput Then logProgress("FileCopy " & $archDir & "\bootmgr -> " & $ISODir, $ret)
		updateProgress("% -> Copying Necessary Files", 4)
		$ret = FileCopy($archDir & "\boot\etfsboot.com", $fullDestination, 1)
		If $logOutput Then logProgress("FileCopy " & $archDir & "\boot\etfsboot.com -> " & $fullDestination, $ret)
		updateProgress("% -> Copying Necessary Files", 4)
		$ret = DirCopy($archDir & "\boot", $ISODir & "\boot", 1)
		If $logOutput Then logProgress("DirCopy " & $archDir & "\boot -> " & $ISODir & "\boot", $ret)
		updateProgress("% -> Copying Necessary Files", 6)
		$ret = DirCopy($archDir & "\EFI", $ISODir & "\EFI", 1)
		If $logOutput Then logProgress("DirCopy " & $archDir & "\EFI -> " & $ISODir & "\EFI", $ret)
		updateProgress("% -> Copying Necessary Files", 6)
		$ret = FileCopy($archDir & "\winpe.wim", $fullDestination, 1)
		If $logOutput Then logProgress("FileCopy " & $archDir & "\winpe.wim -> " & $fullDestination, $ret)
		updateProgress("% -> Copying Necessary Files", 6)
		$ret = FileCopy($archDir & "\winpe.wim", $ISODir & "\sources\boot.wim", 1)
		If $logOutput Then logProgress("FileCopy " & $archDir & "\winpe.wim -> " & $ISODir & "\sources\boot.wim", $ret)
		updateProgress("% -> Copying Necessary Files", 6)
		$ret = RunWait('"' & $toolsDir & '\imagex.exe" /mountrw "' & $fullDestination & '\winpe.wim" 1 "' & $mountDir & '"', $toolsDir, @SW_SHOW)
		If $logOutput Then logProgress($toolsDir & '\imagex.exe /mountrw ' & $fullDestination & '\winpe.wim 1 ' & $mountDir, @error, @extended)
		updateProgress("% -> Copying Necessary Files", 4)
		$ret = DirCopy($servicingDir, $mountDir & "\Windows", 1)
		If $logOutput Then logProgress("DirCopy " & $servicingDir & " -> " & $mountDir & "\Windows", $ret)
		updateProgress("% -> Copying Necessary Files", 6)
		$ret = DirCopy($toolsDir, $mountDir & "\Windows", 1)
		If $logOutput Then logProgress("DirCopy " & $toolsDir & " -> " & $mountDir & "\Windows", $ret)
		$ret = FileCopy($archDir & "\bootsect.exe", $mountDir & "\Windows\System32", 1)
		If $logOutput Then logProgress("FileCopy " & $archDir & "\bootsect.exe -> " & $mountDir & "\Windows\System32", $ret)
		updateProgress("% -> Running Custom Commands", 6)
		#endregion Copy necessary files
		#region Custom Scripts
		$ret = FileDelete($ISODir & "\boot\bootfix.bin")
		If $logOutput Then logProgress("FileDelete " & $ISODir & "\boot\bootfix.bin", $ret)
		updateProgress("% -> Installing Drivers...")
		For $i = 1 To $folderArray[0]
			#comments-start Debug Info for inf file inclusion
				$fileArray = _FileListToArray($infDir & "\" & $folderArray[$i], "*.inf", 1)
				_ArrayDisplay($fileArray)
				MsgBox(0, "", '"' & $PEToolsDir & '\peimg.exe /inf="' & $infDir & '\' & $folderArray[$i] & '\*.inf" ' & $mountDir & '\Windows')
			#comments-end Debug Info for inf file inclusion
			$ret = RunWait('"' & $PEToolsDir & '\peimg.exe" /inf="' & $infDir & '\' & $folderArray[$i] & '\*.inf" "' & $mountDir & '\Windows"', $PEToolsDir, @SW_SHOW)
			If $logOutput Then logProgress($PEToolsDir & '\peimg.exe /inf="' & $infDir & '\' & $folderArray[$i] & '\*.inf" ' & $mountDir & '\Windows', $ret, @error)
		Next
		updateProgress("% -> Running Custom Commands", 6)
		$ret = DirCopy($customScriptsDir, $mountDir & "\Windows\System32", 1)
		If $logOutput Then logProgress("DirCopy " & $customScriptsDir & " -> " & $mountDir & "\Windows\System32", $ret)
		$ret = FileDelete($mountDir & "\Windows\System32\startrestore.cmd")
		If $logOutput Then logProgress("FileDelete " & $mountDir & "\Windows\System32\startrestore.cmd", $ret)
		$ret = FileDelete($mountDir & "\Windows\System32\restore.exe")
		If $logOutput Then logProgress("FileDelete " & $mountDir & "\Windows\System32\restore.exe", $ret)
		$ret = FileDelete($mountDir & "\Windows\System32\restore.ini")
		If $logOutput Then logProgress("FileDelete " & $mountDir & "\Windows\System32\restore.ini", $ret)
		updateProgress("% -> Writing Files Back To ISO")
		#endregion Custom Scripts
		#region Commit Changes
		$ret = RunWait('"' & $PEToolsDir & '\peimg.exe" /prep /f "' & $mountDir & '\Windows"', $PEToolsDir, @SW_SHOW)
		If $logOutput Then logProgress($PEToolsDir & '\peimg.exe" /prep /f "' & $mountDir & '\Windows"', $ret, @error)
		updateProgress("% -> Writing Files Back To ISO", 3)
		$ret = RunWait('"' & $toolsDir & '\imagex.exe" /unmount /commit "' & $mountDir & '"', $toolsDir, @SW_SHOW)
		If $logOutput Then logProgress($toolsDir & '\imagex.exe /unmount /commit ' & $mountDir, $ret, @error)
		updateProgress("% -> Writing Files Back To ISO", 8)
		$ret = RunWait('"' & $toolsDir & '\imagex.exe" /boot /export "' & $fullDestination & '\winpe.wim" 1 "' & $fullDestination & '\final.wim"', $toolsDir, @SW_SHOW)
		If $logOutput Then logProgress($toolsDir & '\imagex.exe /boot /export ' & $fullDestination & '\winpe.wim 1 ' & $fullDestination & '\final.wim', $ret, @error)
		updateProgress("% -> Writing Files Back To ISO", 6)
		$ret = FileCopy($fullDestination & "\final.wim", $ISODir & "\sources\boot.wim", 1)
		If $logOutput Then logProgress("FileCopy " & $fullDestination & "\final.wim -> " & $ISODir & "\sources\boot.wim", $ret)
		updateProgress("% -> Building final image...", 6)
		$ret = RunWait('"' & $PEToolsDir & '\oscdimg.exe" -n -lImageX -o -h -b"' & $bootImagePath & '" "' & $ISODir & '" "' & $fullDestination & '\imagex.iso"', $PEToolsDir, @SW_SHOW)
		If $logOutput Then logProgress($PEToolsDir & '\oscdimg.exe -n -lImageX -o -h -b' & $bootImagePath & ' ' & $ISODir & ' ' & $fullDestination & '\imagex.iso', $ret, @error)
		updateProgress("% -> Moving Completed ISO", 8)
		#endregion Commit Changes
		$ret = FileCopy($fullDestination & "\imageX.iso", $DestinationDir & "\", 1)
		If $logOutput Then logProgress("FileCopy " & $fullDestination & "\imageX.iso -> " & $DestinationDir & "\", $ret)
		updateProgress("% -> Deleting Temporary Files", 4)
		$ret = DirRemove($fullDestination, 1)
		If $logOutput Then logProgress("DirRemove " & $fullDestination, $ret)
		ProgressSet(100, "Done", "Completed!")
		Sleep(3000)
		ProgressOff()
		$msg = $GUI_EVENT_CLOSE
	Else
		MsgBox(48, "ISO Creation", "ISO Creation aborted!")
	EndIf
EndFunc   ;==>BuildISO
#endregion ISO Creation Functions

#region General Functions
Func createFunc($argsString, $progressIncrement = 2)
	Local $fileHandle = 0
	Local $stringArray = StringSplit($argsString, ",")
	Local $aArgs[$stringArray[0]]
	$aArgs[0] = "CallArgArray"
	For $i = 2 To $stringArray[0]
		$aArgs[$i - 1] = Execute($stringArray[$i])
	Next
	$ret = Call($stringArray[1], $aArgs)
	If $logOutput Then
		$fileHandle = FileOpen($DestinationDir & "\creationlog.txt", 1)
		FileWrite($DestinationDir & "\creationlog.txt", $stringArray[1] & " returned with code: " & $ret & " and @error returned: " & @error & @CRLF & @CRLF)
		FileClose($fileHandle)
	EndIf
	ProgressSet($progressAmount, $progressAmount & "% -> " & $stringArray[1])
	$progressAmount += $progressIncrement
EndFunc   ;==>createFunc

Func logProgress($funcName, $errorCode, $secErrorCode = 0)
	Local $fileHandle = 0
	$fileHandle = FileOpen($DestinationDir & "\creationlog.txt", 1)
	FileWrite($DestinationDir & "\creationlog.txt", $funcName & " returned with code: " & $errorCode & " and @error returned: " & $secErrorCode & @CRLF & @CRLF)
	FileClose($fileHandle)
EndFunc   ;==>logProgress

Func updateProgress($progressString = "% -> Progress", $progressIncrement = 2)
	ProgressSet($progressAmount, $progressAmount & $progressString)
	$progressAmount += $progressIncrement
EndFunc   ;==>updateProgress

Func IsVisible($handle)
	If BitAND(WinGetState($handle), 2) Then
		Return 1
	Else
		Return 0
	EndIf
EndFunc   ;==>IsVisible

Func modCallingWindow($title, $state)
	Local $activeWindowList = WinList()
	Local $windowNumber = 0
	For $i = 1 To $activeWindowList[0][0]
		If $activeWindowList[$i][0] <> "" And IsVisible($activeWindowList[$i][1]) Then
			Local $windowTitle = StringTrimLeft($activeWindowList[$i][0], StringLen($activeWindowList[$i][0]) - StringLen($title))
			If $windowTitle = $title Then
				$windowNumber = $i
				WinSetState($activeWindowList[$i][0], "", $state)
			EndIf
		EndIf
	Next
EndFunc   ;==>modCallingWindow
#endregion General Functions